<template>
    <a-table :dataSource="dataSource" :columns="columns" bordered :pagination="false">
        <template #bodyCell="{ column, text, record }">
            <template v-if="column.dataIndex === 'flowWorkStatus'">
                <div>
                    <span v-if="text == '已结束'"><a-tag color="green">{{ text }}</a-tag></span>
                    <span v-if="text == '进行中'"><a-tag color="orange">{{ text }}</a-tag></span>
                    <span v-if="text == '已关闭'"><a-tag>{{ text }}</a-tag></span>
                </div>
            </template>
            <template v-if="column.dataIndex === 'action'">
                <div>
                    <span>
                        <a-button type="link" @click="openDetail(record)">查看详细信息</a-button>
                        <a-button type="link" @click="exReport(record)">生成报告</a-button>
                        <AntdZh>
                            <a-popconfirm title="确定删除该条记录 ?" @confirm="doCancel(record)">
                                <a-button type="link" danger>取消</a-button>
                            </a-popconfirm>
                        </AntdZh>
                    </span>
                </div>
            </template>
        </template>
    </a-table>
    <AntdZh>
        <a-pagination v-model:current="query.pageNum" v-model:pageSize="query.pageSize" :total="total" @change="getList" />
    </AntdZh>
    <a-drawer width="70%" title="详细信息" v-if="open" v-model:visible="open">
        <template #extra>
            <a-button v-if="!showPort" type="primary" @click="showPort = true">报告预览</a-button>
            <a-button v-if="showPort" type="primary" @click="showPort = false">返回</a-button>
        </template>
        <!-- 步骤条 -->
        <a-steps :current="current">
            <a-step v-for="item in node" :title="item.name" />
        </a-steps>
        <a-divider></a-divider>
        <!-- 详细信息 -->
        <workPermitDetail v-if="!showPort" :reportId="selectedReport"></workPermitDetail>
        <!-- 预览 -->
        <div v-if="showPort">
            <FileList :fileIds="fileIds"></FileList>
        </div>
    </a-drawer>
</template>
<script setup>
import { exportList } from "@/api/design/export.js";
import PermitApi from "@/api/salvage/workPermit.js";
import FlowWorks from "@/api/flow/works";
import workPermitDetail from '../detail';
import useApp from "@/hooks";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const { $, proxy } = useApp();
const total = ref(1)
const selectedReport = ref(false);
const open = ref(false)
const showPort = ref(false)
// const props = defineProps({
//     result: Array
// })
const fileIds = ref({
    id: '',
    type: '许可证'
})
const query = ref({
    order: "DESC",
    pageNum: 1,
    pageSize: 10,
    sortBy: "id",
    initiator: ''
})
const current = ref()
const node = ref()
const dataSource = ref()
const columns = [
    {
        title: '作业地点',
        dataIndex: 'wellId',
        key: 'wellId',
        width: '30%'
    },
    {
        title: '填报单位',
        dataIndex: 'unit',
        key: 'unit',
        width: '20%'
    },
    {
        title: '状态',
        dataIndex: 'flowWorkStatus',
        key: 'flowWorkStatus',
        width: '10%'
    },
    {
        title: '操作',
        dataIndex: 'action',
        key: 'action',
    }
];

// 取消
function doCancel(obj) {
    console.log(obj);
    // 流程取消
    FlowWorks.deleteOne(obj.id).then(res => {
        // 许可信息删除
        PermitApi.deleteOne(obj.businessId).then(res => {
            $.showToast({
                title: '删除成功',
                icon: 'success'
            })
            getList()
        })
    })
}
// 获取列表
function getList() {
    let uid = localStorage.getItem('userId')
    if (userStore.isAdmin !== true) { //不是超级管理员情况
        query.value.initiator = uid
    }
    FlowWorks.queryList(query.value).then((response) => {
        dataSource.value = response.content;
        dataSource.value.forEach(element => {
            element.unit = JSON.parse(element.flowParam).unit
            element.wellId = JSON.parse(element.flowParam).wellId
        });
        total.value = response.totalElements;
    });
}
getList();
// 打开详细面板
function openDetail(e) {
    node.value = e.nodeList
    for (let index = 0; index < e.nodeList.length; index++) {
        if (e.nodeList[index].id == e.currentNodeId) {
            current.value = index
        }
    }
    selectedReport.value = e.businessId;
    fileIds.value.id = e.businessId
    open.value = true;
}
// 报告导出
function exReport(e) {
    let queryPort = { schemeId: e.businessId, templateFileId: "", }
    const queryTemplate = { templateDesc: "许可证", templateVersion: "" };
    exportList(queryTemplate).then((r) => {
        queryPort.templateFileId = r[r.length - 1].templateFileId;
        window.open(
            `/dev-api/salvage/licence/schemes/export?schemeId=${queryPort.schemeId}&templateFileId=${queryPort.templateFileId}`
        );
    });
}
</script>